package stud.mpgame.flash.scene;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import stud.mpgame.flash.game.Login;
import stud.mpgame.flash.IScene;
import stud.mpgame.network.Message;
import stud.mpgame.network.MessageDescription;
import stud.mpgame.network.NetworkMonitor;
import stud.mpgame.flash.game.LoginMessageDescription;


/**
 * 22.03.2011 23:35
 * @author GreK
 */

class ConnectScene extends Sprite, implements IScene 
{
	public static inline var NAME = "ConnectScene";
	private var networkMonitor:NetworkMonitor;
	private var hostField:TextField;
	private var portField:TextField;
	private var connectButton:TextField;	
	
	public function new() {
		super();
        
		hostField = new TextField();
		hostField.border = true;
		hostField.type = TextFieldType.INPUT;
		hostField.width = 200;
		hostField.height = 18;
		hostField.text = "127.0.0.1";
		hostField.x = hostField.y = 10;
		
		portField = new TextField();
		portField.border = true;
		portField.type = TextFieldType.INPUT;
		portField.width = 200;
		portField.height = 18;
		portField.text = "8090";
		portField.x = 10;
		portField.y = 34;
		
		addChild(hostField);
		addChild(portField);
		
		connectButton = new TextField();
		connectButton.text = "Connect";
		connectButton.selectable = false;
		connectButton.border = true;
		connectButton.type = TextFieldType.DYNAMIC;
		connectButton.width = 160;
		connectButton.height = 18;
		connectButton.x = 30;
		connectButton.y = 58;
		
		var format:TextFormat = new TextFormat();
		format.align = TextFormatAlign.CENTER;
		connectButton.setTextFormat(format);
		
		addChild(connectButton);
		
		connectButton.addEventListener(MouseEvent.CLICK, handleConnectClick);
	}
	
	private function handleConnectClick(e:MouseEvent) {
		var host = hostField.text;
		var port = Std.parseInt(portField.text);
		
		if ( host != "" && port != null ) {
//			Security.loadPolicyFile("xmlsocket://" + host + ":843");
//			
//			socket = new Socket();
//			socket.connect(host, port);
//			
//			socket.addEventListener(Event.CONNECT, handleSocketConnect);
            
            networkMonitor.bind(host, port, 843);
            networkMonitor.addSessionOpenedListener(handleSocketConnect);
		}
	}
	
	private function handleSocketConnect(e:Event) {
		trace("switch to game scene");
		Main.getInstance().setScene(GameScene.NAME, {networkMonitor: networkMonitor});
		MessageDescription.registerDescription(LoginMessageDescription.getInstance());
		var login:Login = new Login("testuser", 1);
		var message:Message = new Message(networkMonitor, LoginMessageDescription.getInstance());
		message.putUserObject(login);
		message.send();
	}
	
	public function getName():String {
		return NAME;
	}
	
	public function init(data:Dynamic):Void {        
        this.networkMonitor = data.networkMonitor;
	}
	
	public function clear():Void {
		
	}
	
	public function update(deltaTime:Int):Void {
		
	}
	
	public function getContentRoot():DisplayObject {
		return this;
	}
}